package com.itfreer.bpm.workflow.option;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.itfreer.bpm.flow.api.IBpmProjectEntity;
import com.itfreer.bpm.history.entity.BpmHistoryEntity;
import com.itfreer.bpm.option.para.OptionInfo;
import com.itfreer.bpm.option.para.TaskInfo;
import com.itfreer.bpm.power.para.UserInfo;
import com.itfreer.bpm.workflow.drive.IWorkFlowDrive;
import com.itfreer.bpm.workflow.utils.BpmHistoryEditUtil;
import com.itfreer.bpm.workflow.utils.BpmHistoryQueryUtil;
import com.itfreer.bpm.workflow.utils.BpmNextTaskUtils;

/**
 * 单节点会签功能提交
 * @author gj
 *
 */
@Component
public class SignMultiTask {
	
	/**
	 * 流程驱动服务
	 */
	@Autowired
	protected IWorkFlowDrive workFlowDrive;
	
	/**
	 * 构建历史帮转工具
	 */
	@Autowired
	BpmHistoryEditUtil historyEditUtl;
	
	
	/**
	 * 审批过程查询接口
	 */
	@Autowired
	BpmHistoryQueryUtil historyQueryUtil;
	
	
	/**
	 * 下一个环节工具
	 */
	@Autowired
	BpmNextTaskUtils nextTaskUtils;
	
	public void signTask(OptionInfo oInfo, IBpmProjectEntity entity, UserInfo user, String o_sassignees,
			BpmHistoryEntity overWrokH, String fromTaskName, String fromTaskID, HashMap<String, Object> ComValue,String historyStatus) {
		//构建历史
		List<UserInfo> toWorkUser=oInfo.getNextTask().get(0).getUser();
		int index=0;
		List<BpmHistoryEntity> nhlst=new ArrayList<BpmHistoryEntity>();
		List<UserInfo> toWorkSelectUser=new ArrayList<UserInfo>();
		for(UserInfo workUser:toWorkUser) {
			if(!workUser.getUserSelected()) continue;
			toWorkSelectUser.add(workUser);
			HashMap<String, Object> where=new HashMap<String, Object>();
			where.put("toptassigneese:like",  workUser.getUserId());
			where.put("taskdefid:=",  oInfo.getNextTask().get(0).getTaskdefinedid());
			where.put("sexeid:=", oInfo.getSexeid());
			/*where.put("taskdefid:=",  oInfo.getNextTask().get(0).getTaskdefinedid());*/
			where.put("historystatus:in",  "1,21,2");//查询项目要么是待办处理，要么是最新待办，要么是最新，不是待办
			List<BpmHistoryEntity> oldh=historyQueryUtil.getWorkEntitys(null, where, null, 1, 1);
			BpmHistoryEntity history1 =null;
			if(oldh!=null && oldh.size()>0) {

				//3、第一个节点历史配置
				 history1 =oldh.get(0);
				//项目来源节点处理
				 if(fromTaskID.equals(history1.getfromtaskdefid())
						 || (history1.getfromtaskdefid()!=null && history1.getfromtaskdefid().contains(fromTaskID))) {
					 
				 }else {
					 if(history1.getfromtaskdefid()==null) {
							//a、设置项目来源信息
							history1.setfromtaskdefname(fromTaskName);
							history1.setfromtaskdefid(fromTaskID);
					 }else {
							//a、设置项目来源信息
							history1.setfromtaskdefname(history1.getfromtaskdefname()+","+fromTaskName);
							history1.setfromtaskdefid(history1.getfromtaskdefname()+","+fromTaskID);
					 }
				 }
				//用户处理
				 if(user.getUserId().equals(history1.getfromassigneesid())
						 || (history1.getfromassigneesid()!=null && history1.getfromassigneesid().contains(user.getUserId()))) {
					 
				 }else {
					 if(history1.getfromassigneesid()==null) {
							//a、设置项目来源信息
							history1.setfromassigneesid(user.getUserId());
							history1.setfromassigneesname(user.getUserName());
					 }else {
							//a、设置项目来源信息
							history1.setfromassigneesid(history1.getfromassigneesid()+","+user.getUserId());
							history1.setfromassigneesname(history1.getfromassigneesname()+","+user.getUserName());
					 }
				 }
				 //组织机构处理
				 if(user.getUserOrgCode().equals(history1.getfromorgid())
						 || (history1.getfromorgid()!=null && history1.getfromorgid().contains(user.getUserOrgCode()))) {
					 
				 }else {
					 if(history1.getfromorgid()==null) {
							//a、设置项目来源信息
							history1.setfromorgid(user.getUserOrgCode());
							history1.setfromorgname(user.getUserOrgName());
					 }else {
							//a、设置项目来源信息
							history1.setfromassigneesid(history1.getfromorgid()+","+user.getUserOrgCode());
							history1.setfromassigneesname(history1.getfromorgname()+","+user.getUserOrgName());
					 }
				 }
			}else {

				//3、第一个节点历史配置
				 history1 =historyEditUtl.CreateHistory(oInfo.getSexeid(), overWrokH.getactdefid(), user, oInfo.getNextTask().get(0), entity);
				//a、设置项目来源信息
				history1.setfromtaskdefname(fromTaskName);
				history1.setfromtaskdefid(fromTaskID);
				
				history1.setfromassigneesid(user.getUserId());
				history1.setfromassigneesname(user.getUserName());
				history1.setfromorgid(user.getUserOrgCode());
				history1.setfromorgname(user.getUserOrgName());
				
				//d、设置项目待审批人信息
				String tsassignees="";
				List<UserInfo> userlst=oInfo.getNextTask().get(0).getUser();
				for(UserInfo iuser:userlst) {
					tsassignees=tsassignees+","+iuser.getUserId()+","+iuser.getUserName()+",";
				}
				
				history1.setToptassigneese(tsassignees);
			}
			//b、设置项目历史状态信息
			if(index<1) {
				history1.sethistorystatus(historyStatus);
			}else {
				history1.sethistorystatus("1");
			}
			index++;
			
			//c、设置项目历史审批人问题
			String sassignees=","+user.getUserId()+","+user.getUserName();
			if(o_sassignees==null) {
				
			}else if(!o_sassignees.contains(sassignees)){
				sassignees=sassignees+","+o_sassignees;
			}else {
				sassignees=o_sassignees;
			}
			history1.setsassignees(sassignees);
			//d、设置项目待审批人信息
			String tsassignees=","+workUser.getUserId()+","+workUser.getUserName()+",";;
			history1.setToptassigneese(tsassignees);
			history1.setSread("0");
			
			//e、设置项目的下一个环节
			history1.setTasktype(workFlowDrive.getTaskStringType(oInfo.getbpmkey(),  oInfo.getNextTask().get(0).getTaskdefinedid()));
			history1.setNexttask(this.nextTaskUtils.getNextTaskInfo(oInfo.getSexeid(), oInfo.getbpmkey(),oInfo.getNextTask().get(0).getTaskdefinedid(),entity).getNextTaskName());
			nhlst.add(history1);
		}
		
		for(TaskInfo itask:oInfo.getcTask()) {
			workFlowDrive.SignMultiSingleTask(oInfo.getSexeid(),toWorkSelectUser,user,itask,ComValue);
		}
		historyEditUtl.addList(nhlst);
	}


	
}
